<template>
  <div class="app-container">
    <!-- 搜索区 -->
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item label="分类名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分类名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="createTimeValue">
        <el-date-picker
          v-model="createTimeValue"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="btnSearch">搜索</el-button>
      </el-form-item>
    </el-form>

    <!--功能区-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-requiredMerchant
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-requiredMerchant
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate()"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-requiredMerchant
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete()"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport()"
        >导出</el-button>
      </el-col>
    </el-row>

    <!-- 数据展示区table-->
    <el-table v-loading="loading" :data="tableData.rows" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分类id" prop="id" width="100" />
      <el-table-column label="分类名称" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="分类图片">
        <template slot-scope="scope">
          <img v-if="scope.row.picture" :src="scope.row.picture" width="64">
          <img v-else src="@/assets/noimage.png" width="64">
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />

      <el-table-column v-requiredMerchant label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.status===0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页条 -->
    <div style="margin-top: 10px;">
      <el-pagination
        v-if="tableData.total>0"
        :current-page="queryParams.page"
        :page-sizes="[10, 20, 30, 40,50,100]"
        :page-size="queryParams.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!--新增修改对话框      :close-on-click-modal 点击对话框外不会关闭   -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false" :before-close="cancelForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类名称" prop="picture">
          <el-upload
            class="avatar-uploader"
            :headers="headers"
            :action="imageUploadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.picture" :src="form.picture" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-button style="margin-left: 300px" @click="cancelForm">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import PageMixin from '@/mixin/PageMixin.js'
import AddOrEditMixin from '@/mixin/AddOrEditMixin.js'
import { dataApi, addApi, editApi, findByIdApi, deleteByIdApi } from '@/api/category.js'
export default {
  mixins: [PageMixin, AddOrEditMixin],
  data() {
    return {
      fetchDataApi: dataApi,
      addApi: addApi,
      editApi: editApi,
      findByIdApi: findByIdApi,
      deleteByIdApi: deleteByIdApi,
      primaryKey: 'id',
      rules: {
        name: [
          { required: true, message: '不允许为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.initFormValue()
  },
  methods: {
    initFormValue() {
      this.form = {
        picture: undefined
      }
    },
    handleExport() {
      this.download('/api/merchclass/excel', this.queryParams, `分类信息_${new Date().getTime()}.xlsx`)
    },
    // 图片上传成功
    handleAvatarSuccess(resp, file) {
      if (resp.code === 200) {
        this.form.picture = resp.data.url
      } else {
        this.$message.error(resp.msg)
      }
    }
  }
}
</script>

<style>
</style>
